<?php
//设置默认参数
$parameter['offset'] = 12;
$parameter['start_page'] = 1;
$parameter['next_page'] = 2;
$parameter['totalCount'] = 0;
$parameter['pageSize'] = 0;
$parameter['start_time'] = 0;
$parameter['end_time'] = 0;

//接收参数
$parameter['catid'] = (int)$_GET['catid'];
$parameter['name'] = empty($_GET['name']) ? '' : trim($_GET['name']);
$parameter['published'] = isset($_GET['published']) ? trim($_GET['published']) : 0;
$parameter['type'] = isset($_GET['type']) ? trim($_GET['type']) : '';
$parameter['page'] = isset($_GET['page']) && (int)$_GET['page'] > 1 ? (int)$_GET['page'] : 1;

//加载类库
XINHUA::lib('NEWS');
XINHUA::lib('Cecho');

//过滤
if(empty($parameter['catid']) || empty($parameter['name'])){
	Cecho::Eecho(401);
}

//组合条件
$where = '';
if($parameter['catid']==27){

	$where =" `catid`=" . $parameter['catid'] . " AND `type` = 0 ";
	$parameter['totalCount'] = NEWS::recommendTotal($where, 27);

}else{

	$where = " `catid`=" . $parameter['catid'] . " AND `status`=2 AND `posids`=0 AND `isdelete`=1 AND `isdisplay`=0 and `newstype`=0 ";
	$parameter['totalCount'] = NEWS::TOTAL($where);

}

//由于只显示整数页，所以分页不用ceil函数了
$parameter['pageSize'] = $pageSize = intval($parameter['totalCount'] / $parameter['offset']);
if ($parameter['type'] == 'before')
{
	if($parameter['page'] > 1) {
		$parameter['start_page'] = $parameter['page'] - 1;
		$parameter['next_page'] = $parameter['page'] + 1;
	}

	if ($parameter['page'] >= $pageSize) {
		$parameter['page'] = 1;
		$parameter['start_page'] = 1;
		$parameter['next_page'] = 2;
	}

}else if ($parameter['type'] == 'after') {
	$parameter['start_page'] = $parameter['page'] - 1;
	$parameter['next_page'] = $parameter['page'] + 1;

	if ($parameter['page'] >= $pageSize) {
		$parameter['page'] = $pageSize;
		$parameter['next_page'] = $pageSize;
		$parameter['start_page'] = $pageSize - 1;
	
	}

	if ($parameter['start_page'] < 1) {
		$parameter['start_page'] = 1;
	}
}

//获取数据
if($parameter['catid'] == 27){

	$data = NEWS::RECOMMEND($where, $parameter['offset'], $parameter['page'], array(), 27, '' , '');

}else{

	$data = NEWS::LISTDATA($where, $parameter['offset'], $parameter['page'], array(), 0, '', '');

}

//处理结果
for($i=0 , $count=count($data) ; $i<$count ; $i++){
	
	$parameter['start_time'] = $data[0]['published'];
	$parameter['end_time'] = $data[$i]['published'];
	$data[$i]['datestr'] = empty($data[$i]['published']) ? '' : date('Y-m-d H:i:s', $data[$i]['published']) ;
	//详情的地址
	$data[$i]['detail'] = __STATIC_IMAGE_URL__.'letv/detailtojson?newsid='.$data[$i]['newsId'];
}

//前一页,后一页
$parameter['before_url'] = __STATIC_IMAGE_URL__ . "letv/categorytojson?type=before&catid=" . $parameter['catid'] . "&name=" . $parameter['name'] . "&published=" . $parameter['start_time'] . "&page=" . $parameter['start_page'];
$parameter['after_url'] =  __STATIC_IMAGE_URL__ . "letv/categorytojson?type=after&catid=" . $parameter['catid'] . "&name=" . $parameter['name'] . "&published=" . $parameter['end_time'] . "&page=" . $parameter['next_page'];
$parameter['list'] = $data;


/*
Access-Control-Allow-Origin: http://api.bob.com
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: FooBar
Content-Type: text/html; charset=utf-8
*/
header("Access-Control-Allow-Origin: http://api.bob.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Expose-Headers: FooBar");
//输出
Cecho::Secho($parameter);


?>